package com.amsp.oa.oademo.service.impl;

import com.amsp.oa.oademo.entity.SafetyTrainingCard;
import com.amsp.oa.oademo.entity.SafetyTrainingEducation;
import com.amsp.oa.oademo.mapper.SafetyTrainingCardMapper;
import com.amsp.oa.oademo.mapper.SafetyTrainingEducationMapper;
import com.amsp.oa.oademo.service.SafetyTrainingCardService;
import com.amsp.oa.oademo.vo.SafetyTrainingCardVO;
import com.amsp.oa.oademo.vo.SafetyTrainingEducationVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class SafetyTrainingCardServiceImpl implements SafetyTrainingCardService {

    @Autowired
    private SafetyTrainingCardMapper cardMapper;

    @Autowired
    private SafetyTrainingEducationMapper educationMapper;

    @Override
    public SafetyTrainingCardVO getById(Long id) {
        SafetyTrainingCard card = cardMapper.selectById(id);
        if (card == null) {
            return null;
        }

        SafetyTrainingCardVO vo = new SafetyTrainingCardVO();
        BeanUtils.copyProperties(card, vo);

        // 查询教育记录
        LambdaQueryWrapper<SafetyTrainingEducation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SafetyTrainingEducation::getCardId, id);
        List<SafetyTrainingEducation> educations = educationMapper.selectList(wrapper);

        for (SafetyTrainingEducation education : educations) {
            SafetyTrainingEducationVO educationVO = new SafetyTrainingEducationVO();
            BeanUtils.copyProperties(education, educationVO);

            switch (education.getEducationType()) {
                case 1:
                    vo.setEducation1(educationVO);
                    break;
                case 2:
                    vo.setEducation2(educationVO);
                    break;
                case 3:
                    vo.setEducation3(educationVO);
                    break;
            }
        }

        return vo;
    }

    @Override
    public IPage<SafetyTrainingCardVO> getPage(Integer current, Integer size, String name, String team) {
        Page<SafetyTrainingCard> page = new Page<>(current, size);

        LambdaQueryWrapper<SafetyTrainingCard> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.hasText(name), SafetyTrainingCard::getName, name)
                .like(StringUtils.hasText(team), SafetyTrainingCard::getTeam, team)
                .orderByDesc(SafetyTrainingCard::getCreateTime);

        IPage<SafetyTrainingCard> cardPage = cardMapper.selectPage(page, wrapper);

        // 转换为VO
        IPage<SafetyTrainingCardVO> voPage = new Page<>(cardPage.getCurrent(), cardPage.getSize(), cardPage.getTotal());
        List<SafetyTrainingCardVO> voList = new ArrayList<>();

        for (SafetyTrainingCard card : cardPage.getRecords()) {
            voList.add(getById(card.getId()));
        }

        voPage.setRecords(voList);
        return voPage;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean saveOrUpdate(SafetyTrainingCardVO vo) {
        // 保存主表
        SafetyTrainingCard card = new SafetyTrainingCard();
        BeanUtils.copyProperties(vo, card);

        boolean success;
        if (card.getId() == null) {
            success = cardMapper.insert(card) > 0;
        } else {
            success = cardMapper.updateById(card) > 0;
        }

        if (!success) {
            return false;
        }

        // 保存教育记录
        if (vo.getEducation1() != null) {
            saveEducation(card.getId(), 1, vo.getEducation1());
        }
        if (vo.getEducation2() != null) {
            saveEducation(card.getId(), 2, vo.getEducation2());
        }
        if (vo.getEducation3() != null) {
            saveEducation(card.getId(), 3, vo.getEducation3());
        }

        return true;
    }

    private void saveEducation(Long cardId, Integer type, SafetyTrainingEducationVO vo) {
        SafetyTrainingEducation education = new SafetyTrainingEducation();
        BeanUtils.copyProperties(vo, education);
        education.setCardId(cardId);
        education.setEducationType(type);

        if (vo.getId() == null) {
            educationMapper.insert(education);
        } else {
            educationMapper.updateById(education);
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean removeById(Long id) {
        // 删除教育记录
        LambdaQueryWrapper<SafetyTrainingEducation> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SafetyTrainingEducation::getCardId, id);
        educationMapper.delete(wrapper);

        // 删除主表
        return cardMapper.deleteById(id) > 0;
    }
} 